package com.lin.mr.maizilaundry.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.base.BaseActivity;
import com.lin.mr.maizilaundry.fragmentmain.FragmentBasket;
import com.lin.mr.maizilaundry.fragmentmain.FragmentHome;
import com.lin.mr.maizilaundry.fragmentmain.FragmentMe;
import com.lin.mr.maizilaundry.fragmentmain.FragmentMore;
import com.lin.mr.maizilaundry.utils.CountEvent;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class HomeActivity extends BaseActivity {
    public static ViewPager viewPager_home;
    private RadioGroup radioGroup_home;
    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    public FragmentAdapter fragmentAdapter;

    private static int homeIndex = 0;
    private static boolean flag = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Tag","Home-OnCreate");

        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_home);
        initViews();

        if (homeIndex == 1) {
            EventBus.getDefault().post(new CountEvent(4));
            viewPager_home.setCurrentItem(homeIndex);
        }

//        EventBus.getDefault().register(this);
    }

    private void initViews() {
        EventBus.getDefault().register(this);
        viewPager_home = (ViewPager) findViewById(R.id.viewPager_home);
        radioGroup_home = (RadioGroup) findViewById(R.id.radioGroup_home);
        fragmentList = new ArrayList<>();

        FragmentHome fragmentHome = new FragmentHome();
        FragmentBasket fragmentBasket = new FragmentBasket();
        FragmentMe fragmentMe = new FragmentMe();
        FragmentMore fragmentMore = new FragmentMore();
        fragmentList.add(fragmentHome);
        fragmentList.add(fragmentBasket);
        fragmentList.add(fragmentMe);
        fragmentList.add(fragmentMore);

        fragmentManager = getSupportFragmentManager();
        fragmentAdapter = new FragmentAdapter(fragmentManager);
        viewPager_home.setAdapter(fragmentAdapter);
        viewPager_home.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //页面滚动时调用
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //页面选中时调用
            @Override
            public void onPageSelected(int position) {
                ((RadioButton) radioGroup_home.getChildAt(position)).setChecked(true);
            }

            //页面滑动状态改变时调用
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        radioGroup_home.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int position = 0;
                switch (checkedId) {
                    case R.id.rb_home:
                        position = 0;
                        break;
                    case R.id.rb_basket:
                        position = 1;
                        break;
                    case R.id.rb_me:
                        position = 2;
                        break;
                    case R.id.rb_more:
                        position = 3;
                        break;
                }
                viewPager_home.setCurrentItem(position);
            }
        });


    }

    class FragmentAdapter extends FragmentPagerAdapter {

        private int mChildCount;

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public void notifyDataSetChanged() {
            mChildCount =  getCount();
            super.notifyDataSetChanged();
        }

        @Override
        public int getItemPosition(Object object) {
            if (mChildCount > 0){
                mChildCount--;
                return POSITION_NONE;
            }
            return super.getItemPosition(object);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);//这里返回的是item，而不是position
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    public void onEventMainThread(CountEvent event) {
        int count = event.getCount();
        if (count == 1)
            viewPager_home.setCurrentItem(count);
        if (count == 2) {
            viewPager_home.setCurrentItem(2);
            viewPager_home.setCurrentItem(3);
            viewPager_home.setCurrentItem(2);
            viewPager_home.setCurrentItem(1);
            viewPager_home.setCurrentItem(0);
            viewPager_home.setCurrentItem(1);
        }
        if (count == 3) { //用户登录或者退出登录时发送
            viewPager_home.setCurrentItem(2);
            viewPager_home.setCurrentItem(3);
            viewPager_home.setCurrentItem(2);
            viewPager_home.setCurrentItem(1);
            viewPager_home.setCurrentItem(0);
            for (int i = 0; i < fragmentList.size(); i++) {
                viewPager_home.setCurrentItem(i + 1);
            }
        }
        if (count == 6){
            Log.e("Tag","HomeActivity中的广播");
            if (viewPager_home.getAdapter() != null) {
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                List<Fragment> fragments = fm.getFragments();
                if(fragments != null && fragments.size() >0){
                    for (int i = 0; i < fragments.size(); i++) {
                        ft.remove(fragments.get(i));
                    }
                }
                ft.commit();
            }
            EventBus.getDefault().post(new CountEvent(5));
            fragmentAdapter.notifyDataSetChanged();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        homeIndex = 0;
        EventBus.getDefault().unregister(this);//反注册EventBus
    }

    //在HomeActivity中按返回键时退回到第一个Fragment，即首页
    public void unLoginForBack() {
        viewPager_home.setCurrentItem(0);
    }


    public static void homeIndex(int index) {
        homeIndex = index;
        flag = true;
    }

    public static void jumpToPage(boolean flags) {
        flag = flags;
    }


    @Override
    protected void onResume() {
        super.onResume();
        if(flag){ //表示点击洗衣篮的悬浮按钮跳转过来的，应该显示洗衣篮页面
            viewPager_home.setCurrentItem(1);
        }
        EventBus.getDefault().post(new CountEvent(4));
        flag = false;
    }

}
